﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using SysAdmin._03_Service;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form
{
    public partial class InitForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(InitForm));
        private Timer _timer = new Timer();
        private bool _isAllowClose = false;

        public InitForm()
        {
            InitializeComponent();
        }

        private void InitForm_Load(object sender, EventArgs e)
        {
            _timer.Interval = 100;
            _timer.Tick += _timer_Tick;
        }

        private void InitForm_Shown(object sender, EventArgs e)
        {
            _timer.Start();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                _timer.Stop();
                bool b = TimeService.Init();
                if (b)
                {
                    using (var db = SqlSugarHelper.GetDb())
                    {
                        int n = db.Queryable<ShopInfoEntity>().Count();
                    }
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    UICommonTool.ShowAlertDialog("初始化失败！请退出程序稍后再试。");
                    DialogResult = DialogResult.Cancel;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog("初始化失败！请退出程序稍后再试。");
                DialogResult = DialogResult.Cancel;
            }
            finally
            {
                _isAllowClose = true;
                Close();
            }
        }

        private void InitForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_isAllowClose)
            {
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }
    }
}
